<?php

namespace App\Services\Purchase;

use App\CodeResponse;
use App\Enum;
use App\Repositories\Basic\ProductRepository;
use App\Repositories\Purchase\PurchaseItemRepository;
use App\Repositories\Purchase\PurchaseOrderRepository;
use App\Services\BaseService;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PurchaseItemService extends BaseService
{

    public function createItem($orderId, $params)
    {
        $this->checkGetResult($params, $orderId);
        $params['order_id'] = $orderId;
        $params['total_price'] = cal_total_price($params['price'], $params['num']);
        DB::beginTransaction();
        try {
            $res = app(PurchaseItemRepository::class)->create($params);
            PurchaseOrderService::getInstance()->saveTotalCostPrice($res['order_id']);
            DB::commit();
            return $res;
        } catch (QueryException $exception) {
            DB::rollBack();
            Log::error($exception->getMessage());
            return false;
        }
    }


    public function updateItem($id, $params)
    {
        $this->checkGetResult($params, 0, $id);
        $params['total_price'] = cal_total_price($params['price'], $params['num']);
        DB::beginTransaction();
        try {
            $res = app(PurchaseItemRepository::class)->update($id, $params);
            PurchaseOrderService::getInstance()->saveTotalCostPrice($res['order_id']);
            DB::commit();
            return $res;
        } catch (QueryException $exception) {
            DB::rollBack();
            Log::error($exception->getMessage());
            return false;
        }

    }

    public function deleteItem($id)
    {
        $params = ['is_enable' => Enum::IS_ENABLE_FALSE];
        DB::beginTransaction();
        try {
            $res = app(PurchaseItemRepository::class)->update($id, $params);
            PurchaseOrderService::getInstance()->saveTotalCostPrice($res['order_id']);
            DB::commit();
            return true;
        } catch (QueryException $exception) {
            DB::rollBack();
            Log::error($exception->getMessage());
            return false;
        }

    }

    protected function checkGetResult($params, $orderId = 0, $id = 0)
    {
        $result = [];

        if ($id) {
            $result['item'] = app(PurchaseItemRepository::class)->where(['is_enable' => Enum::IS_ENABLE_TRUE])->getById($id);
            if (!$result['item']) {
                $this->throwBusinessException(CodeResponse::FAIL, '查询不到订单条目，id 错误');
            }
            $orderId = $result['item']['order_id'];
        }

        if ($orderId) {
            //检查订单有效
            $result['order'] = app(PurchaseOrderRepository::class)->getById($orderId);
            if (!$result['order']) {
                $this->throwBusinessException(CodeResponse::FAIL, '查询不到订单，order_id 或者id 错误');
            }
        }

        //检查产品有效
        $result['product'] = app(ProductRepository::class)->getById($params['product_id']);

        if (!$result['product']) {
            $this->throwBusinessException(CodeResponse::FAIL, '查询不到产品，product_id 错误');
        }
        //检验产品产品是否符合该订单的供应商
        if ($result['product']['supplier_id'] != $result['order']['supplier_id']) {
            $this->throwBusinessException(CodeResponse::FAIL, '供应商不支持该产品');
        }

        return $result;
    }


}